home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 May / Chip Mayıs 2001.iso / prog / share / 33 / data1.cab / Program_Executable_Files / OptimizeIt40D / Tutorial / QuickTourApp.java < prev    next >
Encoding:
Java Source  |  2001-03-11  |  4.8 KB  |  196 lines

  1. package intuitive.quicktour;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import javax.swing.*;
  6. import java.awt.geom.*;
  7. import java.util.*;
  8. import java.net.URL;
  9.  
  10. public class QuickTourApp extends JPanel implements ActionListener {
  11.   Color  colors[] = { Color.black, Color.red, Color.green, Color.blue, Color.yellow };
  12.   ImageIcon optitLogo;
  13.   
  14.   public QuickTourApp() {
  15.     super();
  16.     try {
  17.       UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
  18.     } catch (Exception exc) {
  19.       System.err.println("Error couldn't read L&F: " + exc);
  20.     }   
  21.     
  22.     /*
  23.      * Loading resources does not work the same way on windows and solaris
  24.      */
  25.     URL url = null;
  26.     url = getClass().getClassLoader().getResource("OptimizeIt.gif");
  27.     if(url == null) {
  28.       url = getClass().getResource("OptimizeIt.gif");
  29.     }
  30.     optitLogo = new ImageIcon(url); 
  31.     
  32.     setLayout(null); // Absolute positioning
  33.     addShape();
  34.     addShape();
  35.   }
  36.     
  37.   public void actionPerformed(ActionEvent e) {
  38.     String action = e.getActionCommand();
  39.     if(action.equals("more"))
  40.       addShape();
  41.     else if(action.equals("less"))
  42.       removeLastShape();
  43.   }
  44.  
  45.   public void addShape() {
  46.     int index = getComponentCount() % colors.length;
  47.     QTShape s;
  48.     s = new QTShape(colors[index % colors.length], index * 50, index * 50);
  49.     add(s);
  50.   }
  51.   
  52.   public void removeLastShape() {
  53.     if(getComponentCount() > 0) {
  54.       QTShape s = (QTShape) getComponent(getComponentCount() - 1);
  55.       s.stop();
  56.       repaint(s.getBounds());
  57.       remove(s);
  58.       s.stop();
  59.     }
  60.   }
  61.  
  62.   public void paintComponent(Graphics g) {
  63.     int w = optitLogo.getIconWidth();
  64.     int h = optitLogo.getIconHeight();
  65.     
  66.     g.setColor(Color.lightGray);
  67.     g.fillRect(0,0,getWidth(), getHeight());
  68.     optitLogo.paintIcon(this, g, (getWidth() - w) / 2, (getHeight() - h) / 2);
  69.   }
  70.   
  71.   public static void main(String[] args) {
  72.     try {
  73.       String vers = System.getProperty("java.version");
  74.       if (vers.compareTo("1.1.2") < 0) {
  75.     System.out.println("!!!WARNING: Swing must be run with a " +
  76.                "1.1.2 or higher version VM!!!");
  77.       }
  78.       QuickTourApp myQuickTourApp = new QuickTourApp();
  79.  
  80.       JFrame frame = new JFrame("OptimizeIt quick tour");
  81.       JMenuBar  menuBar = new JMenuBar();
  82.       JMenu     onlyMenu=new JMenu("Control");
  83.       menuBar.add(onlyMenu);
  84.       frame.setJMenuBar(menuBar);
  85.  
  86.       ButtonGroup group = new ButtonGroup();
  87.       JMenuItem rbMenuItem = new JMenuItem("Add object");
  88.       rbMenuItem.setActionCommand("more");
  89.       rbMenuItem.setMnemonic('a');
  90.       group.add(rbMenuItem);
  91.       onlyMenu.add(rbMenuItem);
  92.       rbMenuItem.addActionListener(myQuickTourApp);
  93.       
  94.       rbMenuItem = new JMenuItem("Remove object");
  95.       rbMenuItem.setMnemonic('r');
  96.       rbMenuItem.setActionCommand("less");
  97.       rbMenuItem.addActionListener(myQuickTourApp);
  98.       group.add(rbMenuItem);
  99.       onlyMenu.add(rbMenuItem);
  100.  
  101.       frame.getContentPane().setLayout(new BorderLayout());
  102.       frame.getContentPane().add("Center", myQuickTourApp);
  103.       frame.addWindowListener(new WindowAdapter() {
  104.     public void windowClosing(WindowEvent e) {
  105.       System.exit(0);
  106.     }});
  107.       frame.setSize(300, 300);
  108.       frame.show();
  109.     }
  110.     catch (Throwable t) {
  111.       System.out.println("uncaught exception: " + t);
  112.       t.printStackTrace();
  113.     }
  114.   }
  115. }
  116.  
  117. class QTShape extends JComponent implements ActionListener {
  118.   Color baseColor;
  119.   javax.swing.Timer timer;
  120.   double xWay,yWay;
  121.   
  122.   public QTShape(Color color,int x,int y) {
  123.     setBounds(x,y,15,15);
  124.     this.baseColor = color;
  125.     xWay=1; yWay=1;
  126.     timer = new javax.swing.Timer(1,this);
  127.     timer.addActionListener(this);
  128.     timer.setRepeats(true);
  129.     timer.start();
  130.   }
  131.   
  132.   protected void stop() { 
  133.     timer.stop();
  134.   }
  135.  
  136.   public boolean isOpaque() {
  137.     return true;
  138.   }
  139.  
  140.   public void paintComponent(Graphics g) {
  141.     g.setColor(baseColor);
  142.     g.fill3DRect(0,0,getWidth(),getHeight(), true);
  143.   }
  144.   
  145.   // The only action performed is the tick of the timer
  146.   public void actionPerformed(ActionEvent event) { 
  147.     Component parent = getParent();
  148.     int maxWidth  = parent.getWidth()  - getWidth();
  149.     int maxHeight = parent.getHeight() - getHeight();
  150.     int x=getX(),y=getY();
  151.     
  152.     if (maxWidth > 0 && maxHeight > 0) {
  153.       if (x >= maxWidth) {
  154.     x=maxWidth;
  155.     xWay = -1 * (5 * Math.random());
  156.       } else if (x <= 0) {
  157.     x=0;
  158.     xWay = 1  * (5 * Math.random());
  159.       }
  160.     
  161.       if (y >= maxHeight) {
  162.     y=maxHeight;
  163.     yWay = -1 * (5 * Math.random());
  164.       } else if (y <= 0) {
  165.     y=0;
  166.     yWay =  1 * (5 * Math.random());
  167.       }
  168.     
  169.       x = (int) Math.rint((double)x + xWay);
  170.       y = (int) Math.rint((double)y + yWay);
  171.       
  172.       setLocation(x,y);
  173.     }
  174.   }
  175. }
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.